package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Hfub;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 师梁 on 2015/7/30.
 */
@Service("readUrlBlack2UIService")
public class ReadUrlBlack2UIService {

    ResultSet rs = null;
    BaseDao baseDao = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public String read(String pageCount, String page, String MessageType, String policy_instance, String iDisplayStart, String iDisplayLength) {
        JSONObject json = new JSONObject();
        JSONArray result = new JSONArray();
        init();
        rs = baseDao.executeSQL("select *from Hfub", new Object[]{});
//        rs = baseDao.executeSQL("select *from Hfub where id>" + iDisplayStart + "limit" + iDisplayLength, new Object[]{});

        try {
            int length = rs.getRow();
            List li = new ArrayList();
            if (rs.next()) {
                li.add(rs);
            }
            json.put("iTotalRecords", length);
            json.put("iTotalDisplayRecords", length);
            for (Object li1 : li) {
                Hfub hfub = (Hfub) li1;
                JSONArray one = new JSONArray();
                one.add(hfub.getFilter());
                one.add("<a class='red' href='javascript:void(0)' title='删除' onclick='delectObj(" + hfub.getId() + ")'><i class='icon-trash bigger-130'></i></a>");
                result.add(one);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        SessionFactory sf = HibernateUtil.getSessionFactory();
//        Session session = sf.openSession();
//        session.clear();
//        Query query = session.createQuery("From Hfub");
//        int length = query.list().size();
//        query.setFirstResult(Integer.parseInt(iDisplayStart));
//        query.setMaxResults(Integer.parseInt(iDisplayLength));
//        List li = query.list();
//        session.close();

        json.put("aaData", result);
        close();
        return json.toString();
    }
}